/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.StudentController;
import java.awt.Component;
import java.io.File;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author NICOLAS
 */
public class ChooseTestGUI extends javax.swing.JFrame {
    
    /**
     * Creates new form ChooseTest
     */
    public ChooseTestGUI() {
        
        initComponents();
        this.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        this.setLocationRelativeTo(null);   //center of screen
        
        File file = new File(System.getProperty("user.dir")+"/src/savedTestFiles/");
        File[] fileList = file.listFiles();
        String tempFile;
        
        DefaultListModel listModel = new DefaultListModel();
        for(File f : fileList){
            tempFile = f.getName().replace(".txt","");
            listModel.addElement(tempFile);
        }
        
        lstTests.setModel(listModel);
    }
    
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnCancelTakeTest = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstTests = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        btnTakeTest = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        btnCancelTakeTest.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Cancel24.png"))); // NOI18N
        btnCancelTakeTest.setText("Cancel");
        btnCancelTakeTest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelTakeTestActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(lstTests);

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 0, 14)); // NOI18N
        jLabel1.setText("Please select the test you want to take:");

        btnTakeTest.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/next24.png"))); // NOI18N
        btnTakeTest.setText("START TEST");
        btnTakeTest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTakeTestActionPerformed(evt);
            }
        });

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/directory64.png"))); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(54, Short.MAX_VALUE)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1)
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 261, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(jPanel1Layout.createSequentialGroup()
                            .add(92, 92, 92)
                            .add(jLabel2)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 106, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1Layout.createSequentialGroup()
                        .add(18, 18, 18)
                        .add(btnCancelTakeTest)
                        .add(46, 46, 46)
                        .add(btnTakeTest)))
                .add(50, 50, 50))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(16, Short.MAX_VALUE)
                .add(jLabel2)
                .add(18, 18, 18)
                .add(jLabel1)
                .add(18, 18, 18)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 142, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnTakeTest)
                    .add(btnCancelTakeTest))
                .add(14, 14, 14))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(20, 20, 20)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(24, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(17, 17, 17)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnTakeTestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTakeTestActionPerformed
        // TODO add your handling code here:
        takeTest();
    }//GEN-LAST:event_btnTakeTestActionPerformed

    private void btnCancelTakeTestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelTakeTestActionPerformed
        // TODO add your handling code here:
        cancel();
    }//GEN-LAST:event_btnCancelTakeTestActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ChooseTestGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ChooseTestGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ChooseTestGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ChooseTestGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ChooseTestGUI().setVisible(true);
            }
        });
    }
    
    
    //method called when the user hits the cancel button
    public void cancel(){
        MainGUI main = new MainGUI();           //goes back to the MainGUI (creating a new one)
        main.setVisible(true);                  //makes it visible
        this.dispose();                         //disposes the actual class
    }
    
    
    //method called when the student hits the TakeTest button
    public void takeTest(){
        
        
        //check if the user did select an option from the list
        if (lstTests.getSelectedIndex() >= 0) {
            
            //message to the user asking if he is ready to start
            Boolean start = messageOptionDialog("The Test you have selected will start now. \n \n Please take into account that each section is being timed, and as soon as you start the test the timmer will begin. \n \n Are you ready?", this);
            
            //check if the uer hit the start button, if he did, start the test by initialising the StudentGUI and Controller
            if (start) {
                StudentGUI student = new StudentGUI();
                StudentController controller = new StudentController(student, lstTests.getSelectedValue().toString(), true);
                student.setVisible(true);
                this.dispose();
            }
        } else {
            messageDialogInfo("Please select a Test from the list !", this);
        }
    }
    
    
    //Message dialog asking question to User. This method returns the option selected by the user. return true if the user wants to continue
    public boolean messageOptionDialog(String messageQuestion,Component cmp){
        
        Object[] options = {"Yes, START TEST","No, cancel!"};
        
        int i =JOptionPane.showOptionDialog(cmp,messageQuestion,"Question",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,new javax.swing.ImageIcon(getClass().getResource("/images/userAtt128.png")),options,options[0]);
        
        if (i==0){
            return true;
        }
        else{
            return false;
        }

    }
    
    
    
   public void messageDialogInfo(String message, Component cmp){

         JOptionPane.showMessageDialog(cmp, message, "Information",JOptionPane.INFORMATION_MESSAGE);
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelTakeTest;
    private javax.swing.JButton btnTakeTest;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList lstTests;
    // End of variables declaration//GEN-END:variables
}
